﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml ;
using System.Net;
using System.IO;
using System.Text;
using QZone.crlclr.errors;

namespace QZone.crlclr.album
{
    public class URLLoader
    {
        public XmlDocument xml;
        public URLLoader()
        {
        }
        public XmlDocument getConfig()
        {
            return this.xml;
        }
        public string load(string path)
        {
            string content = string.Empty;

            try
            {

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Stream stream = response.GetResponseStream();

                StreamReader reader = new StreamReader(stream,Encoding.Default);

                content=reader.ReadToEnd();

                content = GB2312ToUTF8(content);
                content = content.Replace("gb2312", "utf-8");

                reader.Close();
            }
            catch (Exception ex)
            {
                string msg =string.Format("加载字符流过程中出错,文件:{0},出错信息为:{1}",path,ex.Message);

                throw new NetLoadError(msg);
            }

            return content;
        }


        public static string GB2312ToUTF8(string gb2312info)
        {
            byte[] bytes = System.Text.Encoding.GetEncoding("gb2312").GetBytes(gb2312info);
            byte[] bytesutf8 = System.Text.Encoding.Convert(System.Text.Encoding.GetEncoding("gb2312"), System.Text.Encoding.UTF8, bytes);
            string utf8info = Encoding.UTF8.GetString(bytesutf8);
            return utf8info;
        } 
    }
}
